#include "inwindow.h"

#include <QDebug>
#include <QMessageBox>
#include <string>

#include "transassistanceqstring2string.h"
#include "ui_inwindow.h"

InWindow::InWindow(DatabaseWindow *console, bool in, QWidget *parent)
    : QDialog(parent),
      ui(new Ui::InWindow),
      console_(console),
      in_(in) {
    ui->setupUi(this);
}

InWindow::~InWindow() {
    delete ui;
}

void InWindow::on_OK_clicked() {
    QString s[10];  // name, unit, standrad, source, amount, unit_price, total_price, time, note, operator
    for (int i = 0; i < 10; ++i) {
        if (ui->table->item(i, 0) != nullptr)
            s[i] = ui->table->item(i, 0)->text();
    }

    if (in_) {  // 入库
        // 先查询物品是否存在
        console_->ExecuteAndUpdateLog(QString("select ID from items where name = \"%1\" and unit = \"%2\" and standard = \"%3\" and source = \"%4\";").arg(s[0], s[1], s[2], s[3]));
        // 如果不存在
        if (console_->ans_.empty()) {
            // 初始化物品
            console_->ExecuteAndUpdateLog(QString("insert into `items`(name, unit, standard, source) values(\"%1\", \"%2\", \"%3\", \"%4\");").arg(s[0], s[1], s[2], s[3]));
            console_->ExecuteAndUpdateLog(QString("select ID from items where name = \"%1\" and unit = \"%2\" and standard = \"%3\" and source = \"%4\";").arg(s[0], s[1], s[2], s[3]));
        }
        std::string items_id = console_->ans_[0][0];  // 物品 id
        // 插入 in 表
        console_->ExecuteAndUpdateLog(QString("insert into `in`(ID, amount, unit_price, total_price, time, note, operator) values(%1, %4, %5, %6, %7, %8, %9);")
                                          .arg(str2qstr(items_id), s[4], s[5], s[6], s[7], s[8], s[9]));
        // in 表 id
        console_->ExecuteAndUpdateLog("select max(in_ID) from `in`;");
        std::string in_id = console_->ans_[0][0];
        // 插入 log 表
        console_->ExecuteAndUpdateLog(QString("insert into `log`(in_ID) values(%1);").arg(str2qstr(in_id)));
        // 更新物品数量
        console_->ExecuteAndUpdateLog(QString("update items set total_amount =  total_amount + %1;").arg(s[4]));
    } else {  // 出库
        // 先查询物品是否存在
        console_->ExecuteAndUpdateLog(QString("select ID, total_amount from items where name = \"%1\" and unit = \"%2\" and standard = \"%3\" and source = \"%4\";").arg(s[0], s[1], s[2], s[3]));
        // 如果不存在
        if (console_->ans_.empty()) {
            QMessageBox::information(this, tr("提示"), tr("未在数据库中找到相关物品"));
            return;
        }
        std::string items_id = console_->ans_[0][0];  // 物品 id
        std::string total_amount = console_->ans_[0][1]; // 物品数量
        // 如果数量不够
        if(std::stoi(total_amount) < s[4].toInt()) {
            QMessageBox::information(this, tr("提示"), tr("物品数量不足"));
            return;
        }
        // 插入 out 表
        console_->ExecuteAndUpdateLog(QString("insert into `out`(ID, amount, unit_price, total_price, time, note, operator) values(%1, %4, %5, %6, %7, %8, %9);")
                                          .arg(str2qstr(items_id), s[4], s[5], s[6], s[7], s[8], s[9]));
        // out 表 id
        console_->ExecuteAndUpdateLog("select max(out_ID) from `out`;");
        std::string out_id = console_->ans_[0][0];
        // 插入 log 表
        console_->ExecuteAndUpdateLog(QString("insert into `log`(out_ID) values(%1);").arg(str2qstr(out_id)));
        // 更新物品数量
        console_->ExecuteAndUpdateLog(QString("update items set total_amount =  total_amount - %1;").arg(s[4]));

    }
    close();
}
